//
//  CharacterCreateScreen.m
//  cocos2dFirstTest
//
//  Created by Daniel Alexander on 6/15/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CharacterCreateScreen.h"
#import "CCTouchDispatcher.h"
#import "AppDelegate.h"


@implementation CharacterCreateScreen

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	CharacterCreateScreen *layer = [CharacterCreateScreen node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
    //CCDirector *director = [CCDirector sharedDirector];
    
	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
        
        //CGFloat screenHeight = [[CCDirector sharedDirector] winSizeInPixels].height;
        CGFloat screenWidth = [[CCDirector sharedDirector] winSizeInPixels].width;
        
        player_name = @"player";
        player_class = @"warrior";
        
        [self addChild:MenuBackgroundLayer.scene];
        
        //SET THE FONT TO BE USED BY THE MENU
        [CCMenuItemFont setFontName:@"font.ttf"];
        
        //"CREATE YOUR CHARACTER" TITLE LABEL
        CCLabelTTF *title = [CCLabelTTF labelWithString:@"Create your Character" fontName:@"font.ttf" fontSize:28];
        title.color = ccc3(255, 255, 0);
        title.position =  ccp(screenWidth / 2, 300);
        [self addChild: title];
        
        //PLAYER NAME DISPLAY/BUTTON TO CHANGE IT
        playerNameButton = [CCMenuItemFont itemFromString:@"player" target:self selector:@selector(GetPlayerName:)];
        [playerNameButton.label setColor:ccc3(255, 255, 0)];
        
        playerNameMenu = [CCMenu menuWithItems:playerNameButton, nil];
        playerNameMenu.position = ccp(screenWidth / 2,180);
        [self addChild:playerNameMenu];
        
        //BACK MENU TO RETURN TO MAIN MENU
        CCMenuItemFont *backMenuItem = [CCMenuItemFont itemFromString:@"back" target:self selector: @selector(backToMenu:)];
        
        CCMenu *menu = [CCMenu menuWithItems:backMenuItem,nil];
        menu.position = ccp(64,48);
        [menu alignItemsVertically];
        [self addChild:menu z: 2];
        
        //PLAY GAME BUTTON/MENU
        CCMenuItemFont *playMenuItem = [CCMenuItemFont itemFromString:@"play" target:self selector:@selector(playGame:)];
        
        CCMenu *playMenu = [CCMenu menuWithItems:playMenuItem, nil];
        playMenu.position = ccp(screenWidth-64,48);
        [playMenu alignItemsVertically];
        [self addChild:playMenu];
        
        //CHARACTER IMAGES
        mageImage = [CCSprite spriteWithFile: @"mage.png"];
        mageImage.scale = 2.0f;
        mageImage.position = ccp(screenWidth / 2, 260 );
        [self addChild:mageImage];
        mageImage.visible = false;
        
        warriorImage = [CCSprite spriteWithFile:@"warrior.png"];
        warriorImage.scale = 2.0f;
        warriorImage.position = ccp(screenWidth / 2,260);
        [self addChild:warriorImage];
        
        CCMenuItemFont *prevChar = [CCMenuItemFont itemFromString:@"<-   " target:self selector: @selector(previousCharacter:)];
        CCMenuItemFont *nextChar = [CCMenuItemFont itemFromString:@"   ->" target:self selector:@selector(nextCharacter:)];
   
        //MENU BUTTONS FOR SELECTING CHARACTER
        CCMenu *characterMenu = [CCMenu menuWithItems:prevChar,nextChar, nil];
        characterMenu.position = ccp(screenWidth / 2,250);
        [characterMenu alignItemsHorizontally];
        [self addChild:characterMenu];
        
	}
	return self;
}
//HANDLE THE CHANGE NAME ALERT VIEW
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1)
    {
        player_name = [(UITextField *)[alertView viewWithTag:42] text];
        [playerNameButton setString:player_name];
    }
}
//POP UP THE CHANGE NAME ALERT VIEW
-(void)GetPlayerName:(id)sender{
    UIAlertView* dialog = [[UIAlertView alloc] init];
	[dialog setDelegate:self];
	[dialog setTitle:@"Enter Name"];
	[dialog setMessage:@" "];
	[dialog addButtonWithTitle:@"Cancel"];
	[dialog addButtonWithTitle:@"OK"];
    
	UITextField *nameField = [[UITextField alloc] initWithFrame:CGRectMake(20.0, 45.0, 245.0, 25.0)];
    [nameField setTag:42];
    nameField.text = player_name;
    [nameField becomeFirstResponder];
	[nameField setBackgroundColor:[UIColor whiteColor]];
	[dialog addSubview:nameField];
	//CGAffineTransform moveUp = CGAffineTransformMakeTranslation(0.0, 20.0);
	//[dialog setTransform: moveUp];
    [dialog setDelegate:self];
	[dialog show];
	[dialog release];
	[nameField release];
 
}

-(void)previousCharacter:(id)sender{
    if (warriorImage.visible == true)
    {
        warriorImage.visible = false;
        mageImage.visible = true;
    }else{
        warriorImage.visible = true;
        mageImage.visible = false;
    }
}

-(void)nextCharacter:(id)sender{
    if (warriorImage.visible == true)
    {
        warriorImage.visible = false;
        mageImage.visible = true;
    }else{
        warriorImage.visible = true;
        mageImage.visible = false;
    }
}

-(void) backToMenu:(id)sender{
    [[CCDirector sharedDirector] replaceScene:[MainMenuLayer scene]];
}

-(void) playGame:(id)sender{
    if (warriorImage.visible == YES)
    {
        player_class = @"warrior";
    }else
    {
        player_class = @"mage";
    }
    [[NSUserDefaults standardUserDefaults] setObject:player_name forKey:@"playername"];
    [[NSUserDefaults standardUserDefaults] setObject:player_class forKey:@"playerclass"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"starting playing");
    [[CCDirector sharedDirector] replaceScene:[PlayingScreen scene]];
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}

@end
